$(function () {
    loadModifyNowGridTable();
});

function getTotalCount(obj){
	var totalCount = obj.getGridParam('records');
	if(totalCount === null || totalCount === ''){
		return 0;
    }
	return totalCount;
}

function loadModifyNowGridTable() {
	$("#modifyNowGridTable").jqGrid({url:"using_batch.action", 
										datatype:"json", 
										height:500, 
										width:1000, 
										colNames:["批次号", "给哪些宝贝安装", "安装时间", "显示位置", "静态推广位", "动态推广位", "更换频率","更换频率编码", "显示位置编码", "配置编码", "给哪些宝贝安装编码"], 
										colModel:[  {name:"BATCH_ID", index:"BATCH_ID",hidden:true},
													{name:"INSTALL_SETTING_DESC", index:"INSTALL_SETTING_DESC", align:"center", width:"90"},
													{name:"START_TIME", index:"START_TIME", align:"center", width:"90"},
													{name:"INSTALL_PLACE_DESC", index:"INSTALL_PLACE_DESC", align:"center", width:"90"},
													{name:"STATIC_NUM_DESC", index:"STATIC_NUM_DESC", align:"center", width:"60"},
													{name:"INTELL_SETTING_DESC", index:"INTELL_SETTING_DESC", align:"center", width:"200"},
													{name:"FREQUENCY_DESC", index:"FREQUENCY_DESC", align:"center", width:"60"},
													{name:"FREQUENCY", index:"FREQUENCY", hidden:true},
													{name:"INSTALL_PLACE", index:"INSTALL_PLACE", hidden:true},
													{name:"PRO_ID", index:"PRO_ID", hidden:true},
													{name:"INSTALL_SETTING", index:"INSTALL_SETTING", hidden:true}] ,    
									  	viewrecords:true, 
									  	rowNum:10, 
									  	rowList:[10, 20, 30], 
								  	    multiselect: true,
   										multiselectWidth: 50,
									  	loadComplete:function () {
										}, 
									    onSelectRow: function (rowid, status) {
									      if (rowid === null) {
									        return;
									      }
									      //var ret = $("#uninstallIntellGridTable").jqGrid("getRowData", rowid);
									    },
									    postData:{
											'pager.totalCount':function(){return getTotalCount($("#modifyNowGridTable"));}
										},
										jsonReader:{root:"dataset", 
													page:"pager.curPageNo", 
													total:"pager.totalPages", 
													records:"pager.totalCount", 
													repeatitems:false
										}, 
										prmNames:{rows:"pager.pageSize", 
												  page:"pager.curPageNo", 
												  sort:"pager.orderBy", 
												  order:"pager.order"
										}, 
										pager:"#modifyNowGridPager", 
										caption:"已经使用的智能推广配置",
										subGrid: true,
										subGridRowExpanded: function(subgrid_id, row_id) {
											var ret = $("#modifyNowGridTable").jqGrid("getRowData", row_id);
											if(ret.INSTALL_SETTING == "someCategory"){
												var subgrid_table_id = subgrid_id+"_t";
												var pager_id = "p_"+subgrid_table_id;
												$("#"+subgrid_id).html("<table id='"+subgrid_table_id+"'></table><div id='"+pager_id+"'></div>");
												$("#"+subgrid_table_id).jqGrid({
														url:"querySomeCategory.action?PRO_ID="+ret.PRO_ID,
														datatype: "json",
														width:200,
														colNames: ["分类名称","分类编码"],
														colModel: [ {name:"CNAME", index:"CNAME", width:"90"},
																	{name:"CID", index:"CID", align:"center",hidden:true}],
													   	rowNum:5,
													   	pager: pager_id,
													    height: '100%',
													    jsonReader:{root:"dataset", 
															page:"pager.curPageNo", 
															total:"pager.totalPages", 
															records:"pager.totalCount", 
															repeatitems:false
														}, 
														prmNames:{rows:"pager.pageSize", 
																  page:"pager.curPageNo", 
																  sort:"pager.orderBy", 
																  order:"pager.order"
														}, 
														postData:{
															'pager.totalCount':function(){return getTotalCount($("#"+subgrid_table_id));}
														}
												});
											}
											else if(ret.INSTALL_SETTING == "custom"){
												var subgrid_table_id = subgrid_id+"_t";
													var pager_id = "p_"+subgrid_table_id;
													$("#"+subgrid_id).html("<table id='"+subgrid_table_id+"'></table><div id='"+pager_id+"'></div>");
													$("#"+subgrid_table_id).jqGrid({
															url:"queryCustom.action?PRO_ID="+ret.PRO_ID,
															datatype: "json",
															width:200,
															colNames: ["图片","宝贝"],
															colModel: [ {name:"IMG", index:"IMG", formatter:formateadorLink1, sortable:false,align:"center", width:"40px"}, 
																		{name:"TITLE", index:"TITLE", formatter:formateadorLink2, align:"center", width:"120"}],
														   	rowNum:5,
														   	pager: pager_id,
														    height: '100%',
														    jsonReader:{root:"dataset", 
																page:"pager.curPageNo", 
																total:"pager.totalPages", 
																records:"pager.totalCount", 
																repeatitems:false
															}, 
															prmNames:{rows:"pager.pageSize", 
																	  page:"pager.curPageNo", 
																	  sort:"pager.orderBy", 
																	  order:"pager.order"
															}, 
															postData:{
																'pager.totalCount':function(){return getTotalCount($("#"+subgrid_table_id));}
															}
													});
											}
											
											//$("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:false,add:false,del:false})
										},
										subGridRowColapsed: function(subgrid_id, row_id) {
											var subgrid_table_id = subgrid_id+"_t";
											$("#"+subgrid_table_id).remove();
										}
										
										
	});
	//$("#uninstallIntellGridTable").jqGrid('navGrid','#uninstallIntellGridPager',{add:false,edit:false,del:false});
}

function formateadorLink1(cellvalue, options, rowObject) {
	return "<img class='item_img' src='"+cellvalue+"' />";
}

function formateadorLink2(cellvalue, options, rowObject) {
	var title = "<span style='text-decoration:underline;font-size:13px;color:blue;'>" + cellvalue + "</span>";
	return "<a href=http://item.taobao.com/item.htm?id=" + rowObject.IID + " target='_blank'>" + title + "</a>";
}

function modifyNow(){
	var rowids=$("#modifyNowGridTable").jqGrid('getGridParam','selarrrow');
	if(rowids==''){
		alert("没有选择要立即更换的智能推广，请从表格中选择要立即更换的智能推广！");
		return;
	}
	
	rowidArray = (''+rowids).split(",");
	var pro_ids='';
	var frequencys='';
	for(var i = 0;i<rowidArray.length;i++){
		var row_data = $("#modifyNowGridTable").jqGrid("getRowData", rowidArray[i]);
		pro_ids += row_data.PRO_ID+',';
		frequencys += row_data.FREQUENCY+',';
	}
	pro_ids = pro_ids.slice(0,pro_ids.length-1);
	frequencys = frequencys.slice(0,frequencys.length-1);
	$.get("modify_now.action", 'PRO_IDS='+pro_ids+'&FREQUENCYS='+frequencys, processModifyNowResponse);
	$("#modifyNowButton").attr("disabled",true);
}


function processModifyNowResponse(data){
  if(Number(data.result.code)<=0){
	alert(data.result.msg);
  }
  alert("自动更换已转入后台进行，预计10分钟内完成更换；您可以在[更换日志]页面查询本次更换的情况。");
  location.href=$('#query_update_log_loaction').val();
}
